﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Application.SystemManager
{
    public class OrderApp
    {

        private readonly OrderRepository _orderRepository;
        private readonly UserRepository _userRepository;
        private readonly TechnologyRepository _techRepository;
        public OrderApp()
        {
            _orderRepository = new OrderRepository();
            _userRepository = new UserRepository();
            _techRepository = new TechnologyRepository();
        }

        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="stage">阶段 1:预计订单  2:产品申请  3:订单</param>
        /// <param name="status"></param>
        /// <param name="customer"></param>
        /// <param name="saler"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        public IEnumerable<OrderInfo> GetList(DateTime start, DateTime end, int stage, int status, string customer, string saler, string number)
        {
            return _orderRepository.GetList(start, end, stage, status, customer, saler, number).OrderByDescending(a => a.CreateTime);
        }

        public Order GetInfo(Guid id)
        {
            return _orderRepository.FindEntity(a => a.Id == id);
        }

        public OrderInfo GetFullInfo(Guid id)
        {
            return _orderRepository.GetInfo(id).FirstOrDefault();
        }

        //新建订单
        public int Insert(Order order)
        {
            return _orderRepository.Insert(order);
        }

        //添加工艺单
        public int insertTech(Technology tech)
        {
            return _techRepository.Insert(tech);
        }

        /// <summary>
        /// 编辑订单
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public int Update(Order order)
        {
            return _orderRepository.Update(order);
        }

        /// <summary>
        /// 获取单位时间内订单数量（用于生成订单编号）
        /// </summary>
        /// <param name="salerID"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="stage"></param>
        /// <returns></returns>
        public int GetOrderCount(Guid salerID, DateTime start, DateTime end)
        {
            return _orderRepository.IQueryable(a => a.UserId == salerID && a.SubmitTime >= start && a.SubmitTime < end && a.Status > 6 && a.Status < 20).ToList().Count;
        }

        /// <summary>
        ///  获取单位时间内成本申请数量（用于任务提醒）
        /// </summary>
        /// <param name="salerID"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public int GetApplyCount(Guid salerID, DateTime start, DateTime end, int status)
        {
            User u = _userRepository.FindEntity(a => a.Id == salerID);
            return _orderRepository.IQueryable(a => (a.UserId == salerID || u.UserName == "admin" || u.UserName == "xiaoshouzongjian") && a.CreateTime >= start && a.CreateTime < end && a.Status == status && a.Stage == 2).ToList().Count;
        }

        /// <summary>
        /// 根据产品获取对应订单列表
        /// </summary>
        /// <param name="pID"></param>
        /// <returns></returns>
        public List<OrderInfo> GetListByProduct(Guid pID)
        {
            List<OrderInfo> ls = _orderRepository.GetListByProduct(pID).ToList();
            List<OrderInfo> lsNew = new List<OrderInfo>();

            //获取所有的OrderID
            List<Guid> lsID = new List<Guid>();
            for (int i = 0; i < ls.Count; i++)
            {
                lsID.Add(ls[i].Id);
            }

            //ID 去重
            HashSet<Guid> lsNewID = new HashSet<Guid>(lsID);

            //获取去重后的订单列表
            foreach (var obj in lsNewID)
            {
                for (int j = 0; j < ls.Count(); j++)
                {
                    if (obj == ls[j].Id)
                    {
                        lsNew.Add(ls[j]);
                        break;
                    }
                }
            }

            return lsNew;
        }
    }
}
